package bdz.projects.inl.core
{
	import flash.utils.getQualifiedClassName;
	
	public dynamic class CoreCollection extends Array
	{
		
		public function CoreCollection()
		{
			super();
		}
		protected function applyValues(xlist:XMLList, dobj:Object):void{
			for each(var xm:XML in xlist){
				push(new dobj(xm));
			}
			var str:String = getQualifiedClassName(dobj).split("::")[1];
			switch(str){
				case "UserVO":
					sortOn("user_name");
				break;
				
				case "ProductVO":
					sortOn("product_name");
				break;
				case "ProjectVO":
					sortOn("project_title");
				break;
				case "DownloadVO":
					sortOn("download_name");
				break;
				case "NewsVO":
					sortOn("news_date", Array.NUMERIC);
				break;
				
			}
		}
		public function dataFieldName(obj:CoreVO):String{
			var str:String = getQualifiedClassName(obj).split("::")[1];
			switch(str){
				case "UserVO":
				return "user_name";
				case "ProductVO":
				return "product_name";
				case "ProjectVO":
				return "project_title";
				case "NewsVO":
				return "news_title";
				case "HomeVO":
				return "home_title";
			}
			return null;
		}
		public function search(val:String):Array{
			var results:Array = new Array();
			var fname:String = dataFieldName(this[0]);
			
			if(fname){
				for each(var cvo:CoreVO in this){
					var pattern:RegExp = new RegExp(val, "i");
					
					var res:int = (cvo[fname] as String).search(pattern);
					if(res != -1){
						results[results.length] = cvo;
					}
					
				}
			}else{
				return null;
			}
			return results;
		}
		public function getItemWithMultipleProp(nameprops:Array, namevalue:Array):CoreVO{
			
			for each(var cvo:CoreVO in this){
				
				var matched:Boolean = true;
				for(var i:Number = 0;i<nameprops.length;i++){
					
					if(cvo[nameprops[i]].toLowerCase() != namevalue[i].toLowerCase()){
						matched = false;
					}
				}
				if(matched){
					return cvo;
				}
			}
			return null;
		}
		public function getItemWithName(nameprop:String, namevalue:String):CoreVO{
			for each(var cvo:CoreVO in this){
				if(cvo[nameprop].toLowerCase() == namevalue.toLowerCase()){
					return cvo;
				}
			}
			return null;
		}
		public function removeItem(prop:String, value:*):void{
			var cnt:Number = 0;
			for each(var cvo:* in this){
				if(cvo[prop] == value){
					splice(cnt, 1);
					break;
				}
				cnt++;
			}
		}
		
	}
}